<template>
	<view class="personal-edit">
		<u-form :model="form" ref="formRef" label-width="80">
			<u-form-item label="姓名">
				<u-input v-model="form.realName" placeholder="请输入姓名"></u-input>
			</u-form-item>
			<u-form-item label="年龄">
				<u-input v-model="form.age" type="number" placeholder="请输入年龄"></u-input>
			</u-form-item>
			<u-form-item label="性别">
				<u-radio-group v-model="form.sex">
					<u-radio :name="1">男</u-radio>
					<u-radio :name="2">女</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="出生日期">
				<u-input v-model="form.birthDay" placeholder="请选择出生日期" @click="showDate = true" readonly></u-input>
				<u-calendar v-model="showDate" @confirm="onDateConfirm"></u-calendar>
			</u-form-item>
			<u-form-item label="手机">
				<u-input v-model="form.phone" placeholder="请输入手机号"></u-input>
			</u-form-item>
			<u-form-item label="年级">
				<u-input v-model="form.userLevel" placeholder="请输入年级"></u-input>
			</u-form-item>
		</u-form>
		<u-button type="primary" class="save-btn" @click="submit">保存</u-button>
	</view>
</template>
<script>
	import http from "../../http.js"
	export default {
		data() {
			return {
				form: {
					id: '',
					userUuid: '',
					userName: '',
					realName: '',
					age: '',
					role: '',
					sex: 1,
					birthDay: '',
					phone: '',
					lastActiveTime: '',
					createTime: '',
					modifyTime: '',
					status: '',
					userLevel: '',
					imagePath: "",
					classes: "", //用户班级//用户头像
				},
				showDate: false,
				defaultAvatar: 'https://cdn.uviewui.com/uview/album/1.jpg'
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				http({
					url: "/api/student/user/current",
					method: "post",
				}).then(res => {
					console.log(res);
					if (res.code === 1) {
						this.form = res.response
					} else {
						uni.showToast({
							title: '获取失败',
							icon: 'none'
						})
					}
				})
			},


			onDateConfirm(e) {
				this.form.birthDay = e[0]
			},
			submit() {
				http({
					url: '/api/student/user/update',
					method: 'POST',
					data: this.form,
				}).then(res => {
					if (res.code === 1) {
						uni.showToast({
							title: '保存成功',
							icon: 'success'
						})
						uni.switchTab({
							url: "/pages/user/user"
						})
					} else {
						uni.showToast({
							title: '保存失败',
							icon: 'none'
						})
					}
				})
			}
		}

	}
</script>
<style scoped>
	.personal-edit {
		padding: 30rpx;
		background: #fff;
		min-height: 100vh;
	}

	.save-btn {
		margin-top: 40rpx;
		width: 100%;
	}
</style>